﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    class UsuarioPanelAlta : PanelGeneral
    {
        private TableLayoutPanel tlpGral;
        private Label lbId;
        private Label lbIdUser;
        private TextBox tbNombre;
        private TextBox tbApellido;
        private Label lbNombre;
        private Label lbApellido;
        private GroupBox gbGral;        
        private static int groupWidth = 800;
        private static int groupHeight = 500;
        private TextBox tbUserName;
        private TextBox tbDNI;
        private Label lbDNI;
        private Label lbUserName;
        private Label lbSenha;
        private TextBox tbSenha;
        private Label lbEmail;
        private Label lbTel;
        private Label lbCelular;
        private Label lbJerarquia;
        private TextBox tbEmail;
        private TextBox tbTel;
        private TextBox tbCel;
        private TableLayoutPanel tlpBotones;
        private Button btSubmit;
        private Button btVolver;
        private int posicionTablaBotones;
        private ComboBox cbJerarquia;
        
        
        public UsuarioPanelAlta(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            InitializeComponent();
            
            this.formatearTableTitulo("ALTA DE USUARIO");

            formatearGroupBox();
            formatTableForm();
            formatearTablaBotones();

            lbIdUser.Text = handler.buscarNuevoUltimoIdUsuarioEnBD().ToString();
            cargarJerarquiaEnLista();
            
            cargarControlsEnTabla();

            gbGral.Controls.Add(tlpGral);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

           
            this.Controls.Add(gbGral);
            this.Controls.Add(tlpBotones);
        }

        private void formatearGroupBox()
        {
            gbGral.Size = new Size(groupWidth, groupHeight);

            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, gbGral.Size.Width), this.InitialRealPointVertical);
        }

        #region Formateo de la tabla del form
        private void formatTableForm()
        {
            //tlpForm.Size = new Size(gbAltaProveedor.Width-10,gbAltaProveedor.Height-300);

            tlpGral.ColumnStyles.Clear();
            for (int i = 0; i < tlpGral.ColumnCount; i++)
            {
                tlpGral.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            }

            tlpGral.RowStyles.Clear();
            for (int i = 0; i < tlpGral.RowCount; i++)
            {
                tlpGral.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }

            //tlpForm.Size = new Size((gbAltaProveedor.Size.Width - 10));

            tlpGral.BackColor = Color.LightCyan;
            tlpGral.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), 30);

        }
        #endregion

        private void formatearTablaBotones()
        {
            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBotones.BackColor = Color.LightCyan;

            posicionTablaBotones = this.InitialRealPointVertical + gbGral.Size.Height + 20;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), posicionTablaBotones);

        }

      

        private void cargarJerarquiaEnLista()
        {
            List<Jerarquia> lstJerarquia = handler.obtenerJerarquiasFromDB();

            foreach(Jerarquia jerarqui in lstJerarquia)
            {
                cbJerarquia.Items.Add(jerarqui.Descripcion);
            }
        }

        private void cargarControlsEnTabla()
        {
            tlpGral.Controls.Add(lbId);
            tlpGral.Controls.Add(lbIdUser);

            tlpGral.Controls.Add(lbNombre);
            tlpGral.Controls.Add(tbNombre);

            tlpGral.Controls.Add(lbApellido);
            tlpGral.Controls.Add(tbApellido);

            tlpGral.Controls.Add(lbDNI);
            tlpGral.Controls.Add(tbDNI);

            tlpGral.Controls.Add(lbUserName);
            tlpGral.Controls.Add(tbUserName);

            tlpGral.Controls.Add(lbSenha);
            tlpGral.Controls.Add(tbSenha);

            tlpGral.Controls.Add(lbEmail);
            tlpGral.Controls.Add(tbEmail);

            tlpGral.Controls.Add(lbTel);
            tlpGral.Controls.Add(tbTel);

            tlpGral.Controls.Add(lbCelular);
            tlpGral.Controls.Add(tbCel);

            tlpGral.Controls.Add(lbJerarquia);
            tlpGral.Controls.Add(cbJerarquia);



        }

        
      

        private void InitializeComponent()
        {
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.lbId = new System.Windows.Forms.Label();
            this.lbIdUser = new System.Windows.Forms.Label();
            this.tbNombre = new System.Windows.Forms.TextBox();
            this.tbApellido = new System.Windows.Forms.TextBox();
            this.lbNombre = new System.Windows.Forms.Label();
            this.lbApellido = new System.Windows.Forms.Label();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.tbUserName = new System.Windows.Forms.TextBox();
            this.tbDNI = new System.Windows.Forms.TextBox();
            this.lbDNI = new System.Windows.Forms.Label();
            this.lbUserName = new System.Windows.Forms.Label();
            this.lbSenha = new System.Windows.Forms.Label();
            this.tbSenha = new System.Windows.Forms.TextBox();
            this.lbEmail = new System.Windows.Forms.Label();
            this.lbTel = new System.Windows.Forms.Label();
            this.lbCelular = new System.Windows.Forms.Label();
            this.lbJerarquia = new System.Windows.Forms.Label();
            this.tbEmail = new System.Windows.Forms.TextBox();
            this.tbTel = new System.Windows.Forms.TextBox();
            this.tbCel = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btSubmit = new System.Windows.Forms.Button();
            this.btVolver = new System.Windows.Forms.Button();
            this.cbJerarquia = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // tlpGral
            // 
            this.tlpGral.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 11;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.Size = new System.Drawing.Size(700, 400);
            this.tlpGral.TabIndex = 0;
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdUser
            // 
            this.lbIdUser.AutoSize = true;
            this.lbIdUser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdUser.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdUser.Location = new System.Drawing.Point(0, 0);
            this.lbIdUser.Name = "lbIdUser";
            this.lbIdUser.Size = new System.Drawing.Size(100, 23);
            this.lbIdUser.TabIndex = 0;
            this.lbIdUser.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // tbNombre
            // 
            this.tbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbNombre.Location = new System.Drawing.Point(0, 0);
            this.tbNombre.MaxLength = 50;
            this.tbNombre.Name = "tbNombre";
            this.tbNombre.Size = new System.Drawing.Size(250, 30);
            this.tbNombre.TabIndex = 0;
            // 
            // tbApellido
            // 
            this.tbApellido.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbApellido.Location = new System.Drawing.Point(0, 0);
            this.tbApellido.MaxLength = 50;
            this.tbApellido.Name = "tbApellido";
            this.tbApellido.Size = new System.Drawing.Size(200, 30);
            this.tbApellido.TabIndex = 0;
            // 
            // lbNombre
            // 
            this.lbNombre.AutoSize = true;
            this.lbNombre.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNombre.Location = new System.Drawing.Point(0, 0);
            this.lbNombre.Name = "lbNombre";
            this.lbNombre.Size = new System.Drawing.Size(100, 23);
            this.lbNombre.TabIndex = 0;
            this.lbNombre.Text = "Nombre";
            this.lbNombre.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbApellido
            // 
            this.lbApellido.AutoSize = true;
            this.lbApellido.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbApellido.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbApellido.Location = new System.Drawing.Point(0, 0);
            this.lbApellido.Name = "lbApellido";
            this.lbApellido.Size = new System.Drawing.Size(250, 23);
            this.lbApellido.TabIndex = 0;
            this.lbApellido.Text = "Apellido";
            this.lbApellido.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(200, 100);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Alta de Usuario";
            // 
            // tbUserName
            // 
            this.tbUserName.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbUserName.Location = new System.Drawing.Point(0, 0);
            this.tbUserName.MaxLength = 30;
            this.tbUserName.Name = "tbUserName";
            this.tbUserName.Size = new System.Drawing.Size(200, 30);
            this.tbUserName.TabIndex = 0;
            // 
            // tbDNI
            // 
            this.tbDNI.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbDNI.Location = new System.Drawing.Point(0, 0);
            this.tbDNI.MaxLength = 8;
            this.tbDNI.Name = "tbDNI";
            this.tbDNI.Size = new System.Drawing.Size(100, 30);
            this.tbDNI.TabIndex = 0;
            // 
            // lbDNI
            // 
            this.lbDNI.AutoSize = true;
            this.lbDNI.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbDNI.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbDNI.Location = new System.Drawing.Point(0, 0);
            this.lbDNI.Name = "lbDNI";
            this.lbDNI.Size = new System.Drawing.Size(100, 23);
            this.lbDNI.TabIndex = 0;
            this.lbDNI.Text = "DNI";
            this.lbDNI.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbUserName
            // 
            this.lbUserName.AutoSize = true;
            this.lbUserName.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUserName.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUserName.Location = new System.Drawing.Point(0, 0);
            this.lbUserName.Name = "lbUserName";
            this.lbUserName.Size = new System.Drawing.Size(100, 23);
            this.lbUserName.TabIndex = 0;
            this.lbUserName.Text = "Nombre de usuario";
            this.lbUserName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbSenha
            // 
            this.lbSenha.AutoSize = true;
            this.lbSenha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbSenha.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbSenha.Location = new System.Drawing.Point(0, 0);
            this.lbSenha.Name = "lbSenha";
            this.lbSenha.Size = new System.Drawing.Size(100, 23);
            this.lbSenha.TabIndex = 0;
            this.lbSenha.Text = "Contraseña";
            this.lbSenha.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbSenha
            // 
            this.tbSenha.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbSenha.Location = new System.Drawing.Point(0, 0);
            this.tbSenha.MaxLength = 8;
            this.tbSenha.Name = "tbSenha";
            this.tbSenha.Size = new System.Drawing.Size(100, 30);
            this.tbSenha.TabIndex = 0;
            // 
            // lbEmail
            // 
            this.lbEmail.AutoSize = true;
            this.lbEmail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbEmail.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbEmail.Location = new System.Drawing.Point(0, 0);
            this.lbEmail.Name = "lbEmail";
            this.lbEmail.Size = new System.Drawing.Size(100, 23);
            this.lbEmail.TabIndex = 0;
            this.lbEmail.Text = "e-Mail";
            this.lbEmail.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbTel
            // 
            this.lbTel.AutoSize = true;
            this.lbTel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbTel.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbTel.Location = new System.Drawing.Point(0, 0);
            this.lbTel.Name = "lbTel";
            this.lbTel.Size = new System.Drawing.Size(100, 23);
            this.lbTel.TabIndex = 0;
            this.lbTel.Text = "Teléfono";
            this.lbTel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbCelular
            // 
            this.lbCelular.AutoSize = true;
            this.lbCelular.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCelular.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCelular.Location = new System.Drawing.Point(0, 0);
            this.lbCelular.Name = "lbCelular";
            this.lbCelular.Size = new System.Drawing.Size(100, 23);
            this.lbCelular.TabIndex = 0;
            this.lbCelular.Text = "Celular";
            this.lbCelular.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbJerarquia
            // 
            this.lbJerarquia.AutoSize = true;
            this.lbJerarquia.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbJerarquia.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbJerarquia.Location = new System.Drawing.Point(0, 0);
            this.lbJerarquia.Name = "lbJerarquia";
            this.lbJerarquia.Size = new System.Drawing.Size(100, 23);
            this.lbJerarquia.TabIndex = 0;
            this.lbJerarquia.Text = "Jerarquía";
            this.lbJerarquia.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbEmail
            // 
            this.tbEmail.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbEmail.Location = new System.Drawing.Point(0, 0);
            this.tbEmail.MaxLength = 40;
            this.tbEmail.Name = "tbEmail";
            this.tbEmail.Size = new System.Drawing.Size(250, 30);
            this.tbEmail.TabIndex = 0;
            // 
            // tbTel
            // 
            this.tbTel.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbTel.Location = new System.Drawing.Point(0, 0);
            this.tbTel.MaxLength = 15;
            this.tbTel.Name = "tbTel";
            this.tbTel.Size = new System.Drawing.Size(140, 30);
            this.tbTel.TabIndex = 0;
            // 
            // tbCel
            // 
            this.tbCel.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCel.Location = new System.Drawing.Point(0, 0);
            this.tbCel.MaxLength = 15;
            this.tbCel.Name = "tbCel";
            this.tbCel.Size = new System.Drawing.Size(140, 30);
            this.tbCel.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSize = true;
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 100);
            this.tlpBotones.TabIndex = 0;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.ForeColor = System.Drawing.Color.Black;
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Guardar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // cbJerarquia
            // 
            this.cbJerarquia.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbJerarquia.Font = new System.Drawing.Font("Bodoni MT", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cbJerarquia.FormattingEnabled = true;
            this.cbJerarquia.Location = new System.Drawing.Point(0, 0);
            this.cbJerarquia.Name = "cbJerarquia";
            this.cbJerarquia.Size = new System.Drawing.Size(250, 28);
            this.cbJerarquia.TabIndex = 0;
            this.ResumeLayout(false);

        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btSubmit_Click(object sender, EventArgs e)
        {
            

            if (validarTextboxUsuario())
            {
                if (tbDNI.Text.Length == 8)
                {
                    Usuario usuario = new Usuario();

                    try
                    {
                        cargarTextBoxEnUsuario(usuario);
                    }
                    catch (MiException ex)
                    {
                        //  CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                        MessageBox.Show("Error al dar de alta Usuario - Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                    try
                    {
                        if (handler.buscarUsuarioPorDNI(usuario.Dni) == null)
                        {
                            if (handler.buscarUsuarioPorUserName(usuario.Username) == null)
                            {
                                handler.altaUsuario(usuario);
                                MessageBox.Show("Nuevo usuario creado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                limpiarTextBoxs();
                                lbIdUser.Text = handler.buscarNuevoUltimoIdUsuarioEnBD().ToString();
                            }
                            else
                            {
                                tbUserName.Clear();
                                MessageBox.Show("Ya existe el nombre de usuario - Ingrese \n otro nombre de usuario", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            tbDNI.Clear();
                            MessageBox.Show("Ya existe un usuario con ese DNI - Ingrese \n otro DNI de usuario", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }


                    }
                    catch (MiException ex)
                    {
                        CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                        MessageBox.Show("Error al dar de alta Usuario - Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else 
                {
                    MessageBox.Show("INGRESO DE DATOS INCORRECTO \n Cantida de digitos DNI incorrecto", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }else
            {
                MessageBox.Show("Datos vacios ó incorrectos. Revisar datos necesarios de usuario y cantidad de dígitos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void limpiarTextBoxs()
        {
            tbApellido.Clear();
            tbCel.Clear();
            tbDNI.Clear();
            tbEmail.Clear();
            tbNombre.Clear();
            tbSenha.Clear();
            tbTel.Clear();
            tbUserName.Clear();
            cbJerarquia.SelectedIndex = -1;
        }
        private bool validarTextboxUsuario()
        {
            return (tbUserName.Text.Length>2)&& (tbSenha.Text.Length>2) && ((!string.IsNullOrWhiteSpace(tbDNI.Text)) && (!string.IsNullOrWhiteSpace(tbNombre.Text)) && (!string.IsNullOrWhiteSpace(tbApellido.Text)) && (!string.IsNullOrWhiteSpace(tbUserName.Text)) && (!string.IsNullOrWhiteSpace(tbSenha.Text) )&& (!string.IsNullOrWhiteSpace(tbEmail.Text)) && (cbJerarquia.SelectedItem!=null));
        }

        private void cargarTextBoxEnUsuario(Usuario usuario)
        {
            if (!string.IsNullOrWhiteSpace(lbIdUser.Text)) usuario.IdUsuario = Convert.ToInt16(lbIdUser.Text); else throw new MiException("[btSubmit_Click] Error en ID usuario");

            if (!string.IsNullOrWhiteSpace(tbNombre.Text)) usuario.Nombre = tbNombre.Text; else throw new MiException("[btSubmit_Click] Error en Nombre usuario");

            if (!string.IsNullOrWhiteSpace(tbApellido.Text)) usuario.Apellido = tbApellido.Text; else throw new MiException("[btSubmit_Click] Error en Apellido usuario");

            if (!string.IsNullOrWhiteSpace(tbDNI.Text)) usuario.Dni = tbDNI.Text; else throw new MiException("[btSubmit_Click] Error en DNI usuario");

            if (!string.IsNullOrWhiteSpace(tbUserName.Text)) usuario.Username = tbUserName.Text; else throw new MiException("[btSubmit_Click] Error en Username usuario");

            if (!string.IsNullOrWhiteSpace(tbEmail.Text)) usuario.Email = tbEmail.Text; else throw new MiException("[btSubmit_Click] Error en Email usuario");

            if (!string.IsNullOrWhiteSpace(tbSenha.Text)) usuario.Pass = tbSenha.Text; else throw new MiException("[btSubmit_Click] Error en Pass usuario");

            if (!string.IsNullOrWhiteSpace(tbTel.Text)) usuario.Tel = tbTel.Text; else usuario.Tel = "";

            if (!string.IsNullOrWhiteSpace(tbCel.Text)) usuario.Cel = tbCel.Text; else usuario.Cel = "";

            //if (!string.IsNullOrWhiteSpace(cbJerarquia.SelectedItem.ToString())) usuario.Jerarquia = Convert.ToInt32(cbJerarquia.SelectedItem.ToString().Substring(0, 1)); else throw new MiException("[btSubmit_Click] Error en Jerarquia usuario");
            if (!string.IsNullOrWhiteSpace(cbJerarquia.SelectedItem.ToString())) usuario.Jerarquia = cbJerarquia.SelectedItem.ToString(); else throw new MiException("[btSubmit_Click] Error en Jerarquia usuario");
        }
    }
}
